package admin

import (
	"getshop/component/api_io"
	"getshop/component/paging"
	"getshop/models"
	"github.com/astaxie/beego/orm"
)

// 管理员管理
type Admin struct {
	App
}

// @Title 运营人员列表
// @Description 运营人员列表
// @Param Page     body int    true  "页码"
// @Param PageSize body int    false "页大小，默认：20"
// @Param Account  body string false "账号"
// @Param Name     body string false "姓名"
// @Param Mobile   body string false "手机号"
// @Param Email    body string false "邮箱"
// @Param Status   body string false "状态"
// @Success 0      [{},{}]
// @Failure 50000 业务错误
// @router /get-list [post]
func (c *Admin) GetList() {
	var data struct {
		Page     int
		PageSize int
		Account  string
		Name     string
		Mobile   string
		Email    string
		Status   int8
	}
	ok, requestError := api_io.Input(c.Ctx.Input, &data)
	if !ok {
		c.Data["json"] = requestError
		return
	}
	if data.Page < 1 {
		data.Page = 1
	}
	if data.PageSize > 1000 || data.PageSize <= 0 {
		data.PageSize = 20
	}
	// 构建查询对象
	cols := []string{
		"`operate`.`id`",
		"`operate`.`name`",
		"`operate`.`is_super`",
		"`operate`.`group_id`",
		"`operate`.`status`",
		"`user`.`id` `user_id`",
		"`user`.`mobile`",
	}
	args := make([]interface{}, 0)
	qb, _ := orm.NewQueryBuilder("mysql")
	qb.Select(cols...).
		From("`admin`").
		InnerJoin("`user`").
		On("`operate`.`user_id` = `user`.`id`").
		Where("true")
	if data.Mobile != "" {
		qb.And("`user`.`mobile` = ?")
		args = append(args, data.Mobile)
	}
	if data.Mobile != "" {
		qb.And("`user`.`mobile` = ?")
		args = append(args, data.Mobile)
	}
	if data.Name != "" {
		qb.And("`operate`.`name` = ?")
		args = append(args, data.Name)
	}
	if data.Status != 0 {
		qb.And("`operate`.`status` = ?")
		args = append(args, data.Status)
	}
	qb.OrderBy("`operate`.`id`").Desc().Limit(data.PageSize).Offset((data.Page - 1) * data.PageSize)
	sql := qb.String()
	//查询
	list := make([]struct {
		Id      int
		Name    string
		IsSuper int8
		GroupId int
		Status  int8
		UserId  int
		Mobile  string
	}, 0, data.PageSize)
	outList := make([]map[string]interface{}, 0, data.PageSize)
	o := orm.NewOrm()
	_, err := o.Raw(sql, args...).QueryRows(&list)
	if err != nil {
		c.Data["json"] = api_io.Fail(api_io.ErrBusiness, err.Error(), nil)
		return
	}
	adminModel := models.NewAdmin(o)
	for _, item := range list {
		outList = append(outList, map[string]interface{}{
			"Id":           item.Id,
			"Name":         item.Name,
			"IsSuper":      item.IsSuper,
			"IsSuperText":  adminModel.GetFlagFieldText("IsSuper", item.IsSuper),
			"Status":       item.Status,
			"StatusText":   adminModel.GetFlagFieldText("Status", item.Status),
			"UserIdUserId": item.UserId,
			"Mobile":       item.Mobile,
		})
	}
	isLast := len(list) != data.PageSize
	c.Data["json"] = api_io.Success(paging.PageUtil(data.Page, data.PageSize, isLast, outList), "")
}

// @Title 添加管理员
// @Description 添加管理员
// @Param Account  body string true  "账号"
// @Param Password body string true  "密码"
// @Param Name     body string true  "姓名"
// @Param Mobile   body string true  "手机号"
// @Param Sex      body string true  "性别"
// @Param Email    body string true  "邮箱"
// @Param GroupId  body array  false "所属组"
// @Success 0      {}
// @Failure 50000 业务错误
// @router /add [post]
func (c *Admin) Add() {
	var data struct {
		Account  int
		Password int8
		Name     int8
		Mobile   int8
		Sex      int8
		Email    int8
		GroupId  []int
	}
	ok, requestError := api_io.Input(c.Ctx.Input, &data)
	if !ok {
		c.Data["json"] = requestError
		return
	}
}

// @Title 修改状态
// @Description 修改状态
// @Param Aid    body int true  "管理员ID"
// @Param Status body int true  "状态值"
// @Success 0     {Status:int,StatusText:string}
// @Failure 50000 业务错误
// @router /set-status [post]
func (c *Admin) SetStatus() {
	var data struct {
		Aid    int
		Status int8
	}
	ok, requestError := api_io.Input(c.Ctx.Input, &data)
	if !ok {
		c.Data["json"] = requestError
		return
	}
	o := orm.NewOrm()
	_, err := o.QueryTable("Admin").Filter("Aid", data.Aid).Update(orm.Params{
		"Status": data.Status,
	})
	if err != nil {
		c.Data["json"] = api_io.Fail(api_io.ErrBusiness, "状态切换失败："+err.Error(), nil)
		return
	}
	adminModel := models.NewAdmin(o)
	c.Data["json"] = api_io.Success(map[string]interface{}{
		"Status":     data.Status,
		"StatusText": adminModel.GetFlagFieldText("Status", data.Status),
	}, "")
}
